package com.ww.event.observer;
/**
 *
 **/

import com.ww.event.EventListener;

/**
 * @Author weiwei
 * @Date 2021-08-31 11:37
 * @description 事件发射器 参考-事件驱动架构.png
 **/
public class EventEmitter {

    private EventObservable observable;
    
    public EventEmitter(){this.observable = new EventObservable();}

    /**
     * 添加观察者
     * @param listener
     */
    public void addListener(EventListener listener){
        //需要转换器将EventListener抓化为Observer
        observable.addObserver(new EventListenerObserverAdapter(listener));
    }

    /**
     * 发送事件
     * @param event
     */
    public void emit(Object event){
        observable.notifyObservers(event);
    }

    public static void main(String[] args) {
        EventEmitter eventEmitter = new EventEmitter();

        eventEmitter.addListener(System.out::println);

        eventEmitter.addListener(event -> {
            System.out.println(event.getSource());
        });

        //先进后出 执行顺序
        eventEmitter.emit("Hello,World");
    }
}
